package cn.openpool.chess.client;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author 王居三木超
 * @version 1.0
 * @date 2022/5/27 7:41
 * @description TODO
 **/
public class ChessAuto {

    private Boolean level;
    private ItemRule itemRule = new ItemRule();

    public void setLevel(Boolean level) {
        this.level = level;
    }

    public ChessAuto() {

    }

    public void run() {
        if (!level) {
            comment();
        } else {
            tanxin();
        }
    }

    private void comment() {
        Random random = new Random();
        ArrayList<ChessItem> chessItems = new ArrayList<>();
        loop(random, chessItems);
        while (chessItems.size() == 0) {
            loop(random, chessItems);
        }
        ChessItem moveItem = chessItems.get(random.nextInt(chessItems.size()));
        ClientPoint movePoint = moveItem.getpoint();
        Game.move(movePoint.x, movePoint.y, moveItem.getjLabel());
        ChessWindows.selectLabel = null;
    }

    private void loop(Random random, ArrayList<ChessItem> chessItems) {
        int X;
        int Y;
        ChessType chess;
        ChessItem chessItem;
        do {
            X = random.nextInt(8);
            Y = random.nextInt(8);
            chess = ChessWindows.chess[Y][X];
            chessItem = ChessWindows.chessItem.get(X + "-" + Y);
        } while (chess.getType() != 1);
        ChessWindows.selectLabel = chessItem;
        itemRule.rule(chessItem);
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                ChessItem cache = ChessWindows.chessItem.get(x + "-" + y);
                if (cache.getjLabel().getBorder() != null) {
                    chessItems.add(cache);
                }
            }
        }
    }

    private void tanxin() {
        Random random = new Random();
        ArrayList<ChessItem> chessItems = new ArrayList<>();
        ArrayList<ChessItem> chessItemFiltrate = new ArrayList<>();
        ChessItem nowItem = null;
        loopTanxin(random, chessItems, chessItemFiltrate);
        while (chessItems.size() == 0) {
            loopTanxin(random, chessItems, chessItemFiltrate);
        }
        if (chessItemFiltrate.size() != 0) {
            for (ChessItem item : chessItemFiltrate) {
                if (nowItem == null) {
                    nowItem = item;
                } else {
                    if (rule(item) > rule(nowItem)) {
                        nowItem = item;
                    }
                }
            }
            ClientPoint movePoint = nowItem.getpoint();
            Game.move(movePoint.x, movePoint.y, nowItem.getjLabel());
            ChessWindows.selectLabel = null;
        } else {
            ChessItem moveItem = chessItems.get(random.nextInt(chessItems.size()));
            ClientPoint movePoint = moveItem.getpoint();
            Game.move(movePoint.x, movePoint.y, moveItem.getjLabel());
            ChessWindows.selectLabel = null;
        }
    }


    private Integer rule(ChessItem item) {
        ClientPoint getpoint = item.getpoint();
        ChessType type = ChessWindows.chess[getpoint.y][getpoint.x];
        switch (type) {
            case White_Queen:
                return 50;
            case White_King:
                return 30;
            case White_Knight:
            case White_Bishop:
            case White_Rook:
                return 10;
            case White_Pawn:
                return 5;
            default:
                return 0;
        }
    }

    private void loopTanxin(Random random, ArrayList<ChessItem> chessItems, ArrayList<ChessItem> chessItemFiltrate) {
        int X;
        int Y;
        ChessType chess;
        ChessItem chessItem;
        do {
            X = random.nextInt(8);
            Y = random.nextInt(8);
            chess = ChessWindows.chess[Y][X];
            chessItem = ChessWindows.chessItem.get(X + "-" + Y);
        } while (chess.getType() != 1);
        ChessWindows.selectLabel = chessItem;
        itemRule.rule(chessItem);
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                ChessItem cache = ChessWindows.chessItem.get(x + "-" + y);
                if (cache.getjLabel().getBorder() != null) {
                    chessItems.add(cache);
                }
            }
        }
        for (ChessItem item : chessItems) {
            ClientPoint getpoint = item.getpoint();
            chess = ChessWindows.chess[getpoint.y][getpoint.x];
            if (chess != ChessType.Empty) {
                chessItemFiltrate.add(item);
            }
        }
    }
}
